package homework.qingming.calc.operations;

import homework.qingming.calc.Executable;

import java.math.BigDecimal;

public abstract class BaseOperation implements Executable {

    private Character oprChar;
    private int priority = 0;

    public BaseOperation(char opr) {
        oprChar = opr;
    }

    public BaseOperation(char opr, int p) {
        this(opr);
        priority = p;
    }

    public char getOpr() {
        return oprChar;
    }

    public abstract BigDecimal operation(BigDecimal a, BigDecimal b);

    @Override
    public BigDecimal exec(BigDecimal... v) {
        if(!checkParamsLength(v.length))
            throw new UnsupportedOperationException(toString()+"运算符只允许运算两个参数");

        return operation(v[0], v[1]);
    }

    @Override
    public int getPriority() {
        return priority;
    }

    @Override
    public boolean checkParamsLength(int len) {
        return len == 2;
    }

    @Override
    public String toString() {
        return oprChar.toString();
    }
}
